package metrics

import (
	v1log "gitee.com/scottq/go-framework/src/v1/log"
)

type LogIndicator struct {
	l   v1log.ILog
	ch  chan IndicData
	tag string
}

// NewLogIndicator 指标上报器-日志
func NewLogIndicator(l v1log.ILog, tags ...string) *LogIndicator {
	st := &LogIndicator{
		l:   l,
		ch:  make(chan IndicData, 16),
		tag: "LogIndicator",
	}
	if len(tags) > 0 {
		st.tag = tags[0]
	}
	go st.bgReport()
	return st
}

func (ind *LogIndicator) bgReport() {
	for {
		select {
		case data := <-ind.ch:
			ind.l.Info("[%s]%s: %f %s %+v", ind.tag, data.Name, data.Value, data.Unit, data.Labels)
		}
	}
}

// Report 上报数据
func (ind *LogIndicator) Report(data IndicData) {
	ind.ch <- data
}
